using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MilageMVC.Models;
using MilageMVC.ViewModel;

namespace MilageMVC.Controllers
{
    [Authorize]
    public class VehiclesController : Controller
    {
        private MilageDB _db = new MilageDB();

        //
        // GET: /Vehicles/

        public ActionResult Index()
        {
            var model = _db.Vehicles.ToList<Vehicle>().Where(v => v.UserName == User.Identity.Name);

            List<ViewModel.VehicleViewModel> mymodel = new List<VehicleViewModel>();

            if (model.Count() == 0)
            {
                return RedirectToAction("NoVehicles");
            }


            foreach (var vehicle in model)
            {
                VehicleViewModel vvm = CalculateVehicleMileage(vehicle);

                mymodel.Add(vvm);

            }


            return View(mymodel);
        }

        [HttpGet]
        public ActionResult NoVehicles()
        {
            return View();
        }

        public static VehicleViewModel CalculateVehicleMileage(Vehicle vehicle)
        {
            VehicleViewModel vvm = new VehicleViewModel();

            double totalMileage = 0, totalDistance = 0, totalLiters = 0;

            vvm.VehicleID = vehicle.VehicleID;
            vvm.Make = vehicle.Make;
            vvm.Name = vehicle.Name;
            vvm.Year = vehicle.Year;

            totalDistance = vehicle.Mileages.Sum(x => x.Distance);
            totalLiters = vehicle.Mileages.Sum(x => x.VolumeAmount);

            if ((totalDistance != 0) & (totalLiters != 0))
            {
                totalMileage =((totalLiters / totalDistance) * 100) ;
                vvm.Mileage = Math.Round(totalMileage, 2).ToString();
                vvm.MilesPerImperialGallon = Math.Round((282.481 / totalMileage),2).ToString();
                vvm.MilesPerUSGallon = Math.Round((235.215 / totalMileage), 2).ToString();

            }
            else
            {
                vvm.MilesPerImperialGallon = "Unknown";
                vvm.MilesPerUSGallon = "Unknown";
                vvm.Mileage = "Unknown";
            }




            vvm.Units = "ltr/100 km";
            return vvm;
        }

        //
        // GET: /Vehicles/Details/5

        public ViewResult Details(int id)
        {
            Vehicle vehicle = _db.Vehicles.Single(x => x.VehicleID == id);
            return View(vehicle);
        }


        [HttpGet]
        public ActionResult Create()
        {
            return View();
        }


        //
        // POST: /Vehicles/Create

        [HttpPost]
        public ActionResult Create(Vehicle vehicle)
        {
            if (ModelState.IsValid)
            {
                vehicle.UserName = User.Identity.Name;

                _db.Vehicles.Add(vehicle);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(vehicle);
        }

        //
        // GET: /Vehicles/Edit/5

        public ActionResult Edit(int id)
        {
            Vehicle vehicle = _db.Vehicles.Single(x => x.VehicleID == id);
            return View(vehicle);
        }

        //
        // POST: /Vehicles/Edit/5

        [HttpPost]
        public ActionResult Edit(Vehicle vehicle)
        {
            if (ModelState.IsValid)
            {
                _db.Entry(vehicle).State = EntityState.Modified;
                _db.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(vehicle);
        }

        //
        // GET: /Vehicles/Delete/5

        public ActionResult Delete(int id)
        {
            Vehicle vehicle = _db.Vehicles.Single(x => x.VehicleID == id);
            return View(vehicle);
        }

        //
        // POST: /Vehicles/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            Vehicle vehicle = _db.Vehicles.Single(x => x.VehicleID == id);
            _db.Vehicles.Remove(vehicle);
            _db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Mileage(int id)
        {
            Vehicle v = _db.Vehicles.Single(x => x.VehicleID == id);

            return RedirectToAction("CreateByVehicle", "Mileages", v);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}